#include "stm32f10x.h"
#include <string.h>
#include "freertos.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

#include "UART1.h"
#include "LED.h"
#include "DELAY.h"
#include "string.h"

#include "ADC.h"

#define BUF_SIZE 1024
static uint8_t g_BufferIndex = 0;
static char g_Buffer[BUF_SIZE] = {0};

TaskHandle_t hand1;
QueueHandle_t queue, queue2, queue3;

void read_queue(void);

void task1(void * arg) {
    // -------------接收账号密码------------

    printf3("19\r\n");

    int times = 0;
    char buf[50] = {0};

    while (1) {
        char ch = 0;
        BaseType_t ret = xQueueReceive(queue, &ch, 1000);

        //判断队列是否接收完成蓝牙传来的名字和密码!...=...!
        if (ret == pdTRUE) {
            buf[strlen(buf)] = ch;

            if (ch == '!') {
                times ++;
            }

            if (times == 2) {
                break;
            }
        }

    }


    printf3("44\r\n");

    // ---------------收完账号密码： 切割----------------
    char buf1[25] = {0};
    char buf2[25] = {0};

    for (int i = 1; i < 50 ; i++) {
        if (buf[i] != '=') { //拆分名字字符串
            buf1[i - 1] = buf[i];
        }

        if (buf[i] == '=') { //拆分密码字符串
            int j = i + 1;
            int idx = 0;

            for (; j < strlen(buf) -1; j++) {
                buf2[idx] = buf[j];
                idx++;
            }
        }
    }

    // ---------------拼接AT-> esp01s--------------------


    printf2("AT+CWJAP_DEF=\"%s\",\"%s\"\r\n", buf1, buf2);	//传给esp

    printf3("71\r\n");
    //----------------------构建g_Buffer-------------------------------------

    read_queue();


    //---------------------判断g_Buffer中有没有"ok"------------------------------------------

    //printf3("97\r\n");

    if (strstr(g_Buffer, "OK")) {
        printf3("yes\r\n");
        Turn_On_LED();
    } else {
        printf3("no\r\n");
        Turn_Off_LED();
    }

    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    //printf3("103\r\n");
    //---------------------------------------------------------------------------


    printf2("AT+CIPSTART=\"TCP\",\"47.115.220.165\",9053\r\n");

    read_queue();

    if (strstr(g_Buffer, "OK")) {
        printf3("yes\r\n");
        Turn_On_LED2();
    } else {
        printf3("no\r\n");
        Turn_Off_LED2();
    }

    memset(g_Buffer, 0, sizeof(g_Buffer));
    g_BufferIndex = 0;

    printf2("AT+CIPMODE=1\r\n");

    vTaskDelay(1000);

    printf2("AT+CIPSEND\r\n");

    vTaskDelay(1000);

    //------------读传感器数据----------------------------   
   char send_data[4096] = {0};
   
    while (1) {
        int times = 0;
        uint16_t ret1 = AD_GetValue(ADC_Channel_5);
        printf3("light: %d \r\n", ret1);

        uint16_t ret2 = AD_GetValue(ADC_Channel_4);
        printf3("temperature: %d \r\n", ret2);
        times++;
        vTaskDelay(1000);
        
        sprintf(send_data, "light:%s temperature:%s", (char *)ret1, (char *)ret2);

        if (times == 10) {
			printf3("%s\r\n,",send_data);
            break;
        }
    }

    //----------------向服务器发送数据------------------------
    printf2("POST /0 HTTP/1.1 \r\n\r\ndata:%s", send_data);
    memset(send_data, 0, sizeof(send_data));

    vTaskDelay(1000);

    printf2("+++");

    vTaskDelay(1000);

    printf2("AT+CIPMODE=0\r\n");

    vTaskDelay(1000);

    printf2("AT+CIPCLOSE");

    memset(buf, 0, strlen(buf));//释放buf内存空间
    memset(buf1, 0, strlen(buf1));//释放buf1内存空间
    memset(buf2, 0, strlen(buf2));//释放buf2内存空间

    while (1) {
    }

}


void task2(void * arg) {

    char Buf[200] = {0};

    while (1) {

        char ch = 0;
        BaseType_t ret = xQueueReceive(queue2, &ch, 1000);

        if (ret == pdTRUE) {

            Buf[strlen(Buf)] = ch;

            printf3("%c", ch);

            if (ch != 0) {
                g_Buffer[g_BufferIndex++] = ch;
            }
        }

        memset(Buf, 0, sizeof(Buf));
    }
}

void read_queue(void) {
    int i = 0;

    while (i < 50000000) {
        i++;
    }

    if (strlen(g_Buffer) == 0) {
        printf3("NULL\r\n");
    } else {
        printf3("g_Buffer:\r\n");
        printf3("----------------\r\n");
        printf3("%s\r\n", g_Buffer);
        printf3("----------------\r\n");
    }
}

int main(void) {
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    USART1_Init();
    USART2_Init();
    USART3_Init();
    Delay_Init();
    LED_Init();
    AD_Init();

    queue = xQueueCreate(40, 1);
    queue2 = xQueueCreate(40, 1);
    queue3 = xQueueCreate(40, 1);
    xTaskCreate(task1, "Task1", configMINIMAL_STACK_SIZE * 4, NULL, tskIDLE_PRIORITY + 1, &hand1);
    xTaskCreate(task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
    vTaskStartScheduler();

    while (1) {


    }
}
